package com.example.smartcitydemo.ui.job.fragment

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.blankj.utilcode.util.ToastUtils
import com.example.smartcity_jscs.network.ResultStates.Resource
import com.example.smartcitydemo.R
import com.example.smartcitydemo.base.BaseFragment
import com.example.smartcitydemo.bean.ComPanyDetail
import com.example.smartcitydemo.bean.DeliverPostBean
import com.example.smartcitydemo.bean.JobListBean
import com.example.smartcitydemo.databinding.FragmentJobDeliverBinding
import com.example.smartcitydemo.util.onBackPressed
import com.example.smartcitydemo.viewModel.job.WorkViewModel
import kotlin.math.log

/**
 * @PACKAGE_NAME: com.example.smartcitydemo.ui.job.fragment
 * @NAME: Caleb.F
 * @DATE: 2021/11/18 15:50
 * @Email: 1919796283@qq.com
 */
class JobDeliverFragment : BaseFragment<FragmentJobDeliverBinding>(R.layout.fragment_job_deliver) {
    private val workViewModel: WorkViewModel by viewModels()
    private lateinit var companyData1: ComPanyDetail
    private lateinit var jobData1: JobListBean

    override fun initData() {
        arguments?.run {
            val int = getInt("id", 0)
            workViewModel.getPostDetail(int)
            val int1 = getInt("cpId", 0)
            workViewModel.getCompanyDetail(int1)
        }
    }

    override fun initView(bindView: FragmentJobDeliverBinding, savedInstanceState: Bundle?) {
        bindView.apply {
            toolbarWorkInfoDetail.tvContent.text = "职位详情"
            typeBarBusWorkDetailFirst.typeBarText.text = "职位信息"
            typeBarBusWorkDetailSecond.typeBarText.text = "公司信息"
            // 按钮点击投递简历
            btnPost.setOnClickListener {
                val deliverPostBean =
                    DeliverPostBean(companyData1?.id ?: 0, jobData1?.salary ?: "5000",
                        companyData1?.companyName ?: "")
                workViewModel.postDeliver(deliverPostBean)
                //跳转在此完成
                this@JobDeliverFragment.findNavController()
                    .navigate(R.id.action_navigation_job_deliver_to_navigation_work_second)
            }
        }
    }

    override fun addObservers(bindView: FragmentJobDeliverBinding) {
        workViewModel.companyDetail.observe(viewLifecycleOwner) {
            if (it is Resource.Success) {
                companyData1 = it.data!!
                bindView.companyData = it.data!!
            }
        }
        workViewModel.postDetail.observe(viewLifecycleOwner) {
            if (it is Resource.Success) {
                jobData1 = it.data!!
                bindView.jobData = it.data!!
            }
        }
        workViewModel.jobPostResult.observe(viewLifecycleOwner) {
            Log.d(TAG, "addObservers: $it")
            when (it) {
                is Resource.Success -> {
                    ToastUtils.showShort("成功")
                }
                is Resource.Failure -> {
                    ToastUtils.showShort("投递失败")
                }
            }
        }
    }
}